package com.example.books.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.books.entity.Details;
import com.example.books.entity.User;
import com.example.books.mapper.DetailsMapper;
import com.example.books.mapper.UserMapper;
import com.example.books.service.DetailsService;
import com.example.books.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-06-22
 */


@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    public UserService userServiceimpl;

    @Autowired
    public UserMapper userMapper;

    @Autowired
    public DetailsService detailsServiceimpl;

    @Autowired
    public DetailsMapper Detailsmapper;

    @GetMapping(value = "/LoginAndRegister")
    public String LoginAndRegister()
    {
        return "LoginAndRegister";
    }

    @GetMapping(value = "/AjaxVerification")
    @ResponseBody
    public String AjaxVerification(String username)
    {

        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();

        userQueryWrapper.eq("username",username);

        Integer result = userMapper.selectCount(userQueryWrapper);

        if (result == 0)
        {
            return "可注册";
        }

        return "账号已存在";

    }

    @PostMapping(value = "/Register")
    @ResponseBody
    public String Register(@RequestBody User user)
    {
        System.out.println(user);
        boolean save = userServiceimpl.save(user);

        if (save)
        {
            return "注册成功";
        }

        return "注册失败";
    }

    @PostMapping(value = "/logon")
    public String logon(String username , String userpassword, Model model)
    {

        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();

        userQueryWrapper.eq("username",username).eq("userpassword",userpassword);

        Integer result = userMapper.selectCount(userQueryWrapper);

        if (result == 0)
        {

            model.addAttribute("message","登录失败");

            return "LoginAndRegister";
        }


        return "redirect:index";

    }


    @GetMapping(value = "/index")
    public ModelAndView index(Integer pc ,ModelAndView modelandview)
    {
        if (pc == null)
        {
            pc = 1;

        }

        Page<Details> detailsPage = new Page<>(pc,3);

        QueryWrapper<Details> detailsQueryWrapper = new QueryWrapper<>();

        Detailsmapper.selectPage(detailsPage,detailsQueryWrapper);

        modelandview.addObject("Detailslist", detailsPage.getRecords());
        modelandview.addObject("countpage", detailsPage.getPages());//总页数
        modelandview.addObject("pc", detailsPage.getCurrent());
        modelandview.addObject("hasPrevious",detailsPage.hasPrevious());   //是否有上一页
        modelandview.addObject("hasNext",detailsPage.hasNext());       //是否有下一页

        modelandview.setViewName("index");

        return modelandview;

    }


    @GetMapping(value = "/delete")
    public String delete(Integer id)
    {
        System.out.println(id);

        Detailsmapper.deleteById(id);

        return "redirect:index";
    }

    @GetMapping(value = "/add.html")
    public String addhtml()
    {
        return "add";
    }

    @PostMapping(value = "/add")
    public String add(Details details)
    {
        detailsServiceimpl.save(details);
        return "redirect:index";
    }



}

